//
//  DDMeViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/3/13.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDMeViewController.h"
#import "DDMeHeaderView.h"
#import "DDMeCell.h"
#import "UINavigationBar+Awesome.h"
#import "DDSettingViewController.h"
#import "DDInterestingJobViewController.h"
#import "DDCommunicateBossViewController.h"
#import "DDEditMineInfoViewController.h"
#import "DDActivenessViewController.h"
#import "AppDelegate.h"
#import "DDUserInfoService.h"
#import "DDUserInfoModel.h"
#import "DDAccountBalanceViewController.h"
#import "UIScrollView+APParallaxHeader.h"
#import "DDLoginManager.h"
#define NAVBAR_CHANGE_POINT 50

@interface DDMeViewController ()

@property (nonatomic, strong) DDUserInfoModel   *userInfo;
@property (strong, nonatomic) DDUserInfoService *service;
@property (strong, nonatomic) DDMeHeaderView    *headerView;

@end

@implementation DDMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUpData:nil];
    [self setUpView];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setUpData:) name:NOTI_CATTLE_EDIT_PERSIONINFO object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.navigationItem.title = @"我是牛人";

    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

}

#pragma mark
#pragma mark - Private Method

/**
 *  接受用户信息改变的通知，参数附上被改变后的用户信息
 *
 *  @param notice
 */
- (void)setUpData:(NSNotification *)notice
{
    WEAKSELF
    
    id userinfo = notice.object;
    if (userinfo && [userinfo isKindOfClass:[DDUserInfoModel class]])
    {
        self.userInfo = userinfo;
        [self updateViewInfo];
    }
    else
    {
        self.service = [[DDUserInfoService alloc] init];
        
        [self.service queryUserInfo:^(DDUserInfoModel *model) {
            weakSelf.userInfo = model;
            
            if (model)  {
                [weakSelf hideLoading];
                [weakSelf updateViewInfo];
            }
            else {
                [weakSelf showText:@"获取用户信息失败！"];
            }
        }];
    }
}

- (void)updateViewInfo
{
    [self.headerView.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[DDAPIGenerate imageURLWithPath:self.userInfo.logo]] placeholderImage:[UIImage imageNamed:@"common_avatar"]];
    self.headerView.nameLabel.text = [self.userInfo.name length] > 0 ? self.userInfo.name: @"";
    
    
    if ([self.userInfo.sex length] < 1) {
        self.userInfo.sex = @"保密";
    }
    if ([self.userInfo.edu length] < 1) {
        self.userInfo.edu = @"未填写";
    }
    if ([self.userInfo.experience length] < 1) {
        self.userInfo.experience = @"";
    }
    
    self.headerView.infoLabel.text = [NSString stringWithFormat:@" %@ | %@ | 工作%@",self.userInfo.sex,self.userInfo.edu,self.userInfo.experience];
    
    self.headerView.activenessLabel.text = self.userInfo.activeness;
    self.headerView.completenessLabel.text = [NSString stringWithFormat:@"%@%%",self.userInfo.completeness];
    //  修复header尺寸不对的问题.
    [self.headerView setNeedsLayout];
    [self.headerView layoutIfNeeded];
    self.tableView.tableHeaderView = self.headerView;
    [self.tableView reloadData];
}

- (void)setUpView
{
    
    [self addLeftButtonItemWithImage:[UIImage imageNamed:@"me_icon_share"] action:@selector(meShareClick)];
    [self addRightButtonItemWithImage:[UIImage imageNamed:@"me_icon_setting"] action:@selector(meSettingClick)];
    
    
    self.headerView = [DDMeHeaderView meHeaderView];
    self.headerView.frame = CGRectMake(0, 0, kScreenWidth, 312);

    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49 - 44);
    self.tableView.translatesAutoresizingMaskIntoConstraints = false;
    self.tableView.tableHeaderView = _headerView;

    WEAKSELF
    self.headerView.editInfoBlock = ^{
        DDEditMineInfoViewController *editInfoVC = [[DDEditMineInfoViewController alloc] init];
        editInfoVC.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:editInfoVC animated:YES];
    };
    self.headerView.activenessBlock = ^{
        DDActivenessViewController *active = [[DDActivenessViewController alloc] initWithNibName:@"DDActivenessViewController" bundle:nil];
        active.hidesBottomBarWhenPushed = YES;
        active.userInfo = weakSelf.userInfo;
        [weakSelf.navigationController pushViewController:active animated:YES];
    };
    //我的微简历
    self.headerView.tapMyResume = ^{
        myResumeViewController *remusevc = [[myResumeViewController alloc] init];
        remusevc.hidesBottomBarWhenPushed = true;
        [weakSelf.navigationController pushViewController:remusevc animated:true];
    };
    //
    self.headerView.attentionBlock = ^{
        myResumeViewController *remusevc = [[myResumeViewController alloc] init];
        remusevc.hidesBottomBarWhenPushed = true;
        [weakSelf.navigationController pushViewController:remusevc animated:true];
    };
}


- (NSMutableArray *)getMeTitleArray
{
    NSMutableArray *titles = [[NSMutableArray alloc] init];
//    [titles addObject:@{@"title": @"账户余额",@"image":@"me_icon_account"}];
    [titles addObject:@{@"title": @"感兴趣的职位",@"image":@"me_icon_interesting"}];
    [titles addObject:@{@"title": @"沟通过的Boss",@"image":@"me_icon_chat"}];
    [titles addObject:@{@"title": @"当前为牛人版",@"image":@"me_icon_switch"}];
    return titles;
}

- (void)meShareClick
{
    myResumeViewController *remusevc = [[myResumeViewController alloc] init];
    remusevc.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:remusevc animated:true];
}

- (void)meSettingClick
{
//    DDSettingViewController *setting = [[DDSettingViewController alloc] init];
    mySettingVC *setting = [[mySettingVC alloc] init];//swift
    setting.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:setting animated:true];
}

#pragma mark
#pragma mark - UIScrollView Delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIColor * color = [UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1];
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > NAVBAR_CHANGE_POINT) {
        CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64));
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
    } else {
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
    }
}

#pragma mark - UITableView DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self getMeTitleArray].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"DDMeCell";
    
    DDMeCell *cell = (DDMeCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DDMeCell" owner:self options:nil] lastObject];
    }
    
    NSDictionary *dic = [self getMeTitleArray][indexPath.row];
    cell.titleLabel.text = dic[@"title"];
    cell.iconImage.image = [UIImage imageNamed:dic[@"image"]];
    
    if (indexPath.row == 0 ) {
//        // 账户余额
//        cell.subTitleLabel.text = [NSString stringWithFormat:@"￥%@", self.userInfo.moneybalance];
//        cell.subTitleLabel.textColor = COLOR_FONT_RED;
        // 感兴趣的职位
        cell.subTitleLabel.textColor = COLOR_FONT_DARK_GRAY;
        cell.subTitleLabel.text = self.userInfo.interestedjobscount;
    }  else if (indexPath.row == 2) {
        if ([DDClientInfoManager clientType] == DDClientTypeCattle) {
            cell.titleLabel.text = @"当前为牛人端";
            cell.subTitleLabel.text = @"切换至Boss端";
        } else {
            cell.titleLabel.text = @"当前为Boss端";
            cell.subTitleLabel.text = @"切换至牛人端";
        }
    } else {
        cell.subTitleLabel.text = @"";
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *vc = nil;
    switch (indexPath.row) {
        case 0: {
            
            DDInterestingJobViewController *interetingVC = [[DDInterestingJobViewController alloc] init];
            vc = interetingVC;
        }
            break;
        case 1: {
            DDCommunicateBossViewController *coVC = [[DDCommunicateBossViewController alloc] init];
            vc = coVC;
        }
            break;
        case 2: {
            
            [[LDShowHUB shareInstance] showStaus:@"切换中，请稍候..."];
            DDMeViewController *vc = (DDMeViewController *)self;
            [DDClientInfoManager changeClientType:vc];
            
        }
            break;
            
        default:
            break;
    }
    
    if (vc) {
        vc.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

@end
